meta:
  id: nexon_nar_lzss

seq:
  - id: chunks
    type: chunk
    repeat: eos

types:
  flag_byte:
    seq:
      - id: three_bits
        type: b3
      - id: five_bits
        type: b5
  header:
    seq:
      - id: first_byte
        type: flag_byte
      - id: second_byte
        type: u1
        if: not is_raw
      - id: third_byte
        type: u1
        if: is_extended
    instances:
      is_raw:
        value: first_byte.three_bits == 0
      is_extended:
        value: first_byte.three_bits == 7
      raw_size:
        if: is_raw
        value: 'first_byte.five_bits + 1'
      encoded_offset:
        if: not is_raw and not is_extended
        value: '((first_byte.five_bits << 8) | second_byte) + 1'
      encoded_size:
        if: not is_raw and not is_extended
        value: 'first_byte.three_bits + 2'
      extended_offset:
        if: not is_raw and is_extended
        value: '((first_byte.five_bits << 8) | third_byte) + 1'
      extended_size:
        if: not is_raw and is_extended
        value: 'first_byte.three_bits + second_byte + 2'
  chunk:
    seq:
      - id: token
        type: header
      - id: unenconded
        if: token.is_raw
        size: token.raw_size
